package net.bezrabotnih.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import java.util.List;
import net.bezrabotnih.client.managed.request.ApplicationRequestFactory;
import net.bezrabotnih.client.managed.request.CommentProxy;
import net.bezrabotnih.client.managed.request.JobProxy;
import net.bezrabotnih.client.managed.request.JobRequest;
import net.bezrabotnih.client.managed.request.JobTypeProxy;
import net.bezrabotnih.client.managed.request.UserAccountProxy;
import net.bezrabotnih.client.managed.ui.CommentListEditor;
import net.bezrabotnih.client.managed.ui.JobDetailsView;
import net.bezrabotnih.client.managed.ui.JobEditView;
import net.bezrabotnih.client.managed.ui.JobListView;
import net.bezrabotnih.client.managed.ui.JobMobileDetailsView;
import net.bezrabotnih.client.managed.ui.JobMobileEditView;
import net.bezrabotnih.client.managed.ui.UserAccountListEditor;
import net.bezrabotnih.client.scaffold.ScaffoldApp;
import net.bezrabotnih.client.scaffold.place.CreateAndEditProxy;
import net.bezrabotnih.client.scaffold.place.FindAndEditProxy;
import net.bezrabotnih.client.scaffold.place.ProxyPlace;

public class JobActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public JobActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new JobDetailsActivity((EntityProxyId<JobProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? JobMobileDetailsView.instance() : JobDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<net.bezrabotnih.client.managed.request.JobProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<JobProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        JobEditView.instance().setCreating(true);
        final JobRequest request = requests.jobRequest();
        Activity activity = new CreateAndEditProxy<JobProxy>(JobProxy.class, request, ScaffoldApp.isMobile() ? JobMobileEditView.instance() : JobEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(JobProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new JobEditActivityWrapper(requests, ScaffoldApp.isMobile() ? JobMobileEditView.instance() : JobEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        JobEditView.instance().setCreating(false);
        EntityProxyId<JobProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<JobProxy>(proxyId, requests, ScaffoldApp.isMobile() ? JobMobileEditView.instance() : JobEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(JobProxy proxy) {
                JobRequest request = requests.jobRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new JobEditActivityWrapper(requests, ScaffoldApp.isMobile() ? JobMobileEditView.instance() : JobEditView.instance(), activity, proxyId);
    }
}
